package com.pango.core.utils;

import java.util.List;
import java.util.Map;

import com.pango.system.model.Resource;

/**
 * 动态菜单栏生成
 * 
 */
public class ListtoMenu {

	public static String getEasyuiMultistageTree(
			Map<String, List<Resource>> map) {
		if(map==null||map.size()==0||!map.containsKey("0")){return "不具有任何权限,\n请找管理员分配权限";}
		StringBuffer menuString = new StringBuffer();
		List<Resource> list = map.get("0");
		for (Resource function : list) {
			menuString.append("<div   title=\"" + function.getName()
					+ "\" iconCls=\"" + 111
					+ "\">");
			int submenusize = function.getParentIds() == null ? 0 :function.getParentIds().split(",").length;
			if (submenusize == 0) {
				menuString.append("</div>");
			}
			if (submenusize > 0) {
				menuString.append("<ul class=\"easyui-tree\"  fit=\"false\" border=\"false\">");
			}
			menuString.append(getChildOfTree(function,"1",map));
			if (submenusize > 0) {
				menuString.append("</ul></div>");
			}
		}
		return menuString.toString();
	}
	
	/**
	 * 获取叶子节点  ------树形菜单的叶子节点
	 * @param function
	 * @return
	 */
	private static String getLeafOfTree(Resource function){
		StringBuffer menuString = new StringBuffer();
		String icon = "folder";
		menuString.append("<li iconCls=\"");
		menuString.append(icon);
		menuString.append("\"> <a onclick=\"addTab(\'");
		menuString.append(function.getName());
		menuString.append("\',\'");
		menuString.append(function.getUrl());
		menuString.append("&clickFunctionId=");
		menuString.append(function.getId());
		menuString.append("\',\'");
		menuString.append("\',\'");
		menuString.append(icon);
		menuString.append("\')\"  title=\"");
		menuString.append(function.getName());
		menuString.append("\" url=\"");
		menuString.append(function.getUrl());
		menuString.append("\" href=\"#\" ><span class=\"nav\" >");
		menuString.append(function.getName());
		menuString.append("</span></a></li>");
		return menuString.toString();
	}
	
	/**
	 * 获取树形菜单
	 * @param parent
	 * @param level
	 * @param map
	 * @return
	 */
	private static String getChildOfTree(Resource parent,String level,Map<String, List<Resource>> map){
		StringBuffer menuString = new StringBuffer();
		List<Resource> list = map.get(level);
		for (Resource function : list) {
			if (function.getParentId().equals(parent.getId())){
					menuString.append(getLeafOfTree(function));
			}
		}
		return menuString.toString();
	}
}
